/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.template.freemarker;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Locale;

import com.hyts.template.config.FileAttributeConfig;
import com.hyts.template.strformat.JavaCodeConfig;

import freemarker.template.Configuration;
import lombok.extern.slf4j.Slf4j;

/** 
 * @author LiBo/Alex
 * @since TODO引入特定改变发布版本
 * @version V1.0 
 */
@Slf4j
public class FreemarkerContext {
    

    /**
     * Configuration file 
     */
    private Configuration configuration;
    
    private static final String DEFAULT_TEMPLATE_PATH_NAME = "templates";
    
    //default template at classpath resource directory
    private static final String DEFAULT_TEMPLATE_PATH = JavaCodeConfig.MAVEN_RESOURCE_PATH+DEFAULT_TEMPLATE_PATH_NAME;
    
    /**
     * 
     * @param encoding
     * @param locale
     * @param filePath
     * @return
     * @exception
     */
    public FreemarkerContext createContext(String encoding,Locale locale,String filePath) {
        //validate the configuration
        if(configuration == null)
            configuration = new Configuration();
        configuration.setEncoding(locale, encoding);
        try {
            configuration.setDirectoryForTemplateLoading(Paths.get(filePath).toFile());
        } catch (IOException e) {
            log.info(e.getMessage());
        }
        return this;
    }
    
    /**  
     * @param locale
     * @param filePath
     * @return
     * @exception
     */ 
    public FreemarkerContext createContext(Locale locale,String filePath) {
        //validate the configuration
        if(configuration == null)
            configuration = new Configuration();
        configuration.setEncoding(locale, FileAttributeConfig.DEFAULT_FILE_ENCODING);
        try {
            configuration.setDirectoryForTemplateLoading(Paths.get(filePath).toFile());
        } catch (IOException e) {
            log.info(e.getMessage());
        }
        return this;
    }

    /**  
     * @param clazz
     * @param path
     * @return
     * @exception
     */ 
    public FreemarkerContext createContext(Class clazz,String path) {
        //validate the configuration
        if(configuration == null)
            configuration = new Configuration();
        configuration.setEncoding(Locale.CHINESE, FileAttributeConfig.DEFAULT_FILE_ENCODING);
        try {
        configuration.setClassForTemplateLoading(clazz, path);
           //configuration.setDirectoryForTemplateLoading(Paths.get(filePath).toFile());
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return this;
    }
    
    
    /**  
     * @param filePath
     * @return
     * @exception
     */ 
    public FreemarkerContext createContext(String filePath) {
        //validate the configuration
        if(configuration == null)
            configuration = new Configuration();
        configuration.setEncoding(Locale.CHINESE, FileAttributeConfig.DEFAULT_FILE_ENCODING);
        try {
            configuration.setDirectoryForTemplateLoading(Paths.get(filePath).toFile());
        } catch (IOException e) {
            log.info(e.getMessage());
        }
        return this;
    }
    
    
    /**  
     * @return
     * @exception
     */ 
    public FreemarkerContext createContext() {
        //validate the configuration
        if(configuration == null)
            configuration = new Configuration();
        configuration.setEncoding(Locale.CHINESE, FileAttributeConfig.DEFAULT_FILE_ENCODING);
        try {
            configuration.setDirectoryForTemplateLoading(Paths.get(DEFAULT_TEMPLATE_PATH).toFile());
        } catch (IOException e) {
            log.info(e.getMessage());
        }
        return this;
    }
    
    /**  
     * @return
     * @exception
     */ 
    public Configuration getConfiguration() {
        return configuration;
    }
    
}
